<?php
class PWP_View {

	public function registerResources() {
		$referer = explode('/', $_SERVER['HTTP_REFERER']);
		$referer = substr($referer[3].'/'.$referer[4], 0, 20);
		if ($referer != 'wp-admin/plugins.php') {
			$this->removeResources();
		} else {
			$this->addResources();
		}
	}
	
	public function sendResponse($value) {
		$xmlResponse = new WP_Ajax_Response($value);
		$xmlResponse->send();
	}
	
	public function sendResponseJson($array) {
		echo json_encode($array);
		die();
	}
	
	private function removeResources() {
		$this->unregisterJs('platformaWp-jQuery');
		$this->unregisterJs('platformaWp-jQueryUI');
		$this->unregisterJs('platformaWp-js');
		$this->unregisterCss('platformaWp-jQueryUi-Css');
	}
	
	private function addResources() {
		$this->registerCss("platformaWp-jQueryUi-Css", "platforma-wp/scripts/jquery/css/jquery-ui-1.10.3.custom.min.css");
		$this->registerJs("platformaWp-jQuery", "platforma-wp/scripts/jquery/js/jquery-1.9.1.js");
		$this->registerJs("platformaWp-jQueryUI", "platforma-wp/scripts/jquery/js/jquery-ui-1.10.3.custom.min.js");
		$this->registerJs("platformaWp-js", "platforma-wp/scripts/platformawp.js");
	}
	
	private function unregisterCss($alias) {
		wp_deregister_style($alias);
		wp_dequeue_style($alias);
	}
	
	private function unregisterJs($alias) {
		wp_deregister_script($alias);
		wp_dequeue_script($alias);
	}
	
	private function registerJs($alias, $url) {
		wp_register_script($alias, plugins_url($url), array(), false, true);
		wp_enqueue_script($alias);
	}
	
	private function registerCss($alias, $url) {
		wp_register_style($alias, plugins_url($url));
		wp_enqueue_style($alias);
	}
}
?>
